﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cwtrock.model;
using cwtrock.util;
using System.Data.SqlClient;
using System.Data;

namespace cwtrock.dao.impl
{
    class SysSolverDaoImpl
    {
        private GetConn getConn = new GetConn();
        public int save(SysSolver vou)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("INSERT INTO [SYS_SOLVER_TYPE]");
                sql.Append("([S_TYPE])");
                sql.Append(" VALUES(@keyword)");

                SqlParameter[] parameters ={
                    new SqlParameter("@keyword",SqlDbType.VarChar,50)
                };
                parameters[0].Value = vou.KEYWORD;

                SqlCommand cmd = new SqlCommand(sql.ToString(), getConn.GetCon());
                cmd.Parameters.Add(parameters[0]);
                int rows = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return rows;

            }
            catch (Exception e)
            {
                throw e;
                return 0;
            }
        }

        public int update(SysSolver vou)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("UPDATE [SYS_SOLVER_TYPE]");
                sql.Append(" SET [S_TYPE] = @code");
                sql.Append(" WHERE id = @id");

                SqlParameter[] parameters ={
                new SqlParameter("@id",SqlDbType.Int),   
                new SqlParameter("@code",SqlDbType.VarChar,50)
                };
                parameters[1].Value = vou.KEYWORD;
                parameters[0].Value = vou.ID;

                SqlCommand cmd = new SqlCommand(sql.ToString(), getConn.GetCon());
                cmd.Parameters.Add(parameters[0]);
                cmd.Parameters.Add(parameters[1]);

                int rows = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return rows;

            }
            catch (Exception e)
            {
                throw e;
                return 0;
            }

        }

        public List<SysSolver> getAllSolvers()
        {
            String sql = "SELECT [ID] ,[S_TYPE] FROM [SYS_SOLVER_TYPE]";
            SqlCommand cmd = new SqlCommand(sql.ToString(), getConn.GetCon());
            SqlDataReader reader = cmd.ExecuteReader();
            List<SysSolver> res = new List<SysSolver>();

            while (reader.Read())
            {
                SysSolver cu = new SysSolver();
                cu.ID = Convert.ToInt32(reader[0].ToString());
                cu.KEYWORD = reader[1].ToString();
                res.Add(cu);
            }
            return res;
        }

        public SysSolver getSolver(int id)
        {
            String sql = "SELECT top 1 [ID] ,[S_TYPE] FROM [SYS_SOLVER_TYPE] where id = @id";
            SqlParameter[] parameters = { 
                new SqlParameter("@id",SqlDbType.Int)                             
            };
            parameters[0].Value = id;

            SqlCommand cmd = new SqlCommand(sql.ToString(), getConn.GetCon());
            cmd.Parameters.Add(parameters[0]);

            SqlDataReader reader = cmd.ExecuteReader();
            SysSolver res = null;

            while (reader.Read())
            {
                res = new SysSolver();
                res.ID = int.Parse(reader[0].ToString());
                res.KEYWORD = reader[1].ToString();
            }
            return res;
        }

        public int delSolver(int id)
        {
            StringBuilder sql = new StringBuilder("DELETE FROM [SYS_SOLVER_TYPE] WHERE ");
            sql.Append(" id = @id ");

            SqlParameter[] parameters = { 
                new SqlParameter("@id",SqlDbType.Int)                             
            };
            parameters[0].Value = id;

            SqlCommand cmd = new SqlCommand(sql.ToString(), getConn.GetCon());
            cmd.Parameters.Add(parameters[0]);
            int rows = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return rows;

        }

    }
}
